package cn.snailhd.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {
	
	/**
	 * 域名匹配
	 */
	public final static String REGEX_DOMAIN_NAME = "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/)";
	public final static String REGEX_MOBILE = "0?(13|14|15|17|18|19)[0-9]{9}";
	public final static String REGEX_EMAIL = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
	
	
	private static int grounpIndex =1;
	public static void setGrounpIndex(int index){
		grounpIndex = index;
	}

	
	public static List<String> getList(String input,Pattern pattern){
		List<String> list = new ArrayList();	
		Matcher matcher = pattern.matcher(input);
		while(matcher.find()){
			String str = matcher.group(grounpIndex);
			list.add(str);
		}		
		return list;
	}
	
	public static List<String> getList(String input,Tag...tags){
		List<String> list = new ArrayList();
		
		for(Tag tag:tags){
			for(Pattern pattern:tag.get()){
				List<String> relist  = getList(input, pattern);
					for(String s:relist){
						if(!list.contains(s)){
							list.add(s);
						}
					}
			}			
		}
		
		return list;
	}
	
	public static List<String> getList(String input,String regex){
		Pattern pattern = Pattern.compile(regex);
		return getList(input,pattern);
	}
	
	
	public static List<String> match(String input,String pattern) {
		List<String> list = new ArrayList();
		
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(input);

        if(m.find()){
        	list.add(m.group());
        }
        return list;
	}
	
	

}
